<template>
  <div class="L-margin-top-20">

    <van-cell-group inset>
      <div class="L-form-title-row">
        <div>
          <i class="iconfont icon-yuanzhu" style="color:#1683f7"></i>
          <span class="L-text L-primary-text-color">修改密码</span>
        </div>

      </div>
      <van-field v-model="password" required label="旧密码" placeholder="请输入用户名" />
      <van-field type="password" v-model="newPassword" required label="新密码" placeholder="请输入新密码" />
      <van-field type="password" v-model="confirmPassword" required label="确认密码" placeholder="请输入确认密码" />
    </van-cell-group>

  </div>
  <div class="L-margin-top-40 L-text-align-center">
    <van-button @click="handleSubmit" type="primary" round size="large" class="L-login-btn">提交</van-button>
  </div>
</template>

<script setup lang="ts">
import { apiConfigurable } from "@/request/api/api";
import qs from 'qs'
import { useAuthStore } from '@/stores/userInfo'
import { showLoadingToast, closeToast, showToast } from "vant";
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const password = ref("");
const newPassword = ref("");
const confirmPassword = ref("");

function handleSubmit() {
  if (password.value === "") {
    showToast("请填写旧密码")
    return
  }
  if (newPassword.value === "") {
    showToast("新密码不能为空")
    return
  }
  if (newPassword.value !== confirmPassword.value) {
    showToast("确认密码不正确")
    return
  }
  let data = {
    oldpassword: password.value,
    newpassword: newPassword.value,
    confirmpassword: confirmPassword.value
  }
  apiConfigurable('/api/loginuser.changepwsave.do', qs.stringify(data)).then((res) => {
    //   if (res.code === 0) {

    //     closeToast();
    //   }

    // }).finally(() => {
    // });

     if (res.code === 1) {
      showToast(res.data[0].error_des)
      if (res.data.des && res.data.des.indexOf('登录过期') > 0) {
        this.loginout()
      }
    } else {
      showToast("修改密码成功！");
      const userStore = useAuthStore();
      userStore.clearToken()
      setTimeout(() => {
        sessionStorage.setItem("isLogin", "0");
        window.close()
        router.push({
          path: "/login",
        });
      }, 1000)
    }
  }).catch(res => {
  })
}

</script>

<style scoped></style>